home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / term / fig.trm < prev    next >
Encoding:
Text File  |  1996-01-22  |  13.8 KB  |  497 lines

  1. /*
  2.  * $Id: fig.trm,v 1.39 1995/12/20 21:47:48 drd Exp $
  3.  */
  4.  
  5. /* GNUPLOT - fig.trm */
  6. /*
  7.  * Copyright (C) 1990, 1991, 1992
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted,
  11.  * provided that the above copyright notice appear in all copies and
  12.  * that both that copyright notice and this permission notice appear
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed
  17.  * as patches to released version.
  18.  *
  19.  * This software  is provided "as is" without express or implied warranty.
  20.  *
  21.  * This file is included by ../term.c.
  22.  *
  23.  * This terminal driver supports:
  24.  *  Fig graphics language
  25.  *
  26.  * AUTHORS
  27.  *  Micah Beck, David Kotz
  28.  *
  29.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  30.  *
  31.  */
  32.  
  33. /*
  34.  * Original for Fig code output by Micah Beck, 1989
  35.  * Department of Computer Science, Cornell University
  36.  * Updated by David Kotz for gnuplot 2.0
  37.  * More efficient output by Ian Dall
  38.  * Updated to FIG 2.1 (with color) format by Vivek Khera
  39.  * Updated to FIG 3.1 (higher resolution) format by Ian MacPhedran, Jan 1995
  40.  * Updated to conform to newterm format Ian MacPhedran, Apr 1995
  41.  */
  42.  
  43. #ifndef GOT_DRIVER_H
  44. #include "driver.h"
  45. #endif /* GOT_DRIVER_H */
  46.  
  47. #ifdef TERM_REGISTER
  48. register_term(fig)
  49. #endif /* TERM_REGISTER */
  50.  
  51. #ifdef TERM_PROTO
  52. TERM_PUBLIC void FIG_options __P((void));
  53. TERM_PUBLIC void FIG_init __P((void));
  54. TERM_PUBLIC void FIG_graphics __P((void));
  55. TERM_PUBLIC void FIG_text __P((void));
  56. TERM_PUBLIC void FIG_linetype __P((int linetype));
  57. TERM_PUBLIC void FIG_move __P((unsigned int x, unsigned int y));
  58. TERM_PUBLIC void FIG_vector __P((unsigned int ux, unsigned int uy));
  59. TERM_PUBLIC void FIG_arrow __P((unsigned int sx, unsigned int sy, unsigned int ex, unsigned int ey, TBOOLEAN head));
  60. TERM_PUBLIC void FIG_put_text __P((unsigned int x, unsigned int y, char *str));
  61. TERM_PUBLIC int FIG_justify_text __P((enum JUSTIFY mode));
  62. TERM_PUBLIC int FIG_text_angle __P((int angle));
  63. TERM_PUBLIC void FIG_reset __P((void));
  64. TERM_PUBLIC void FIG_lpoint __P((unsigned int x, unsigned int y, int number));
  65.  
  66. #define GOT_FIG_PROTO
  67. #endif /* TERM_PROTO */
  68.  
  69. #ifndef TERM_PROTO_ONLY
  70. #ifdef TERM_BODY
  71.  
  72. #include "object.h"             /* modified from the XFig distribution */
  73. #define FIG_DEFAULT DEFAULT
  74. #define FIG_ROMAN_FONT (0)    /* actually, the default font */
  75.  
  76. #ifndef FIG_RES
  77. /* This is now 1200 per inch */
  78. #define FIG_RES         (1200)
  79. #endif
  80.  
  81. #define FIG_COORD_SYS   2
  82. #define FIG_ORIENT "Portrait"
  83. /* Could be "Landscape" */
  84. #define FIG_JUST "Center"
  85. /* Could be "Flush Left" */
  86. #define FIG_UNIT "Inches"
  87. /* Could be "Metric" */
  88.  
  89. #define FIG_MAGIC       "#FIG 3.1"
  90. #define FIG_HTIC        (5*FIG_RES/80)
  91. #define FIG_VTIC        (5*FIG_RES/80)
  92. #define FIG_FONT_S      (10)    /* size in points */
  93.  
  94. #define FIG_to_pixel_v(s) ((s)*FIG_RES/72*3/4) /* height of font in pixels */
  95. /* This is fudged to enlarge the drawing area, but gives fairly good results */
  96. #define FIG_to_pixel_h(s) (FIG_to_pixel_v(s)*6/10) /* this is a guess at the width */
  97.  
  98. #define FIG_VCHAR       FIG_to_pixel_v(FIG_FONT_S) /* just for default, */
  99. #define FIG_HCHAR       FIG_to_pixel_h(FIG_FONT_S) /* not really used   */
  100.  
  101. enum FIG_poly_stat {FIG_poly_new, FIG_poly_part};
  102.  
  103. static int FIG_posx;
  104. static int FIG_posy;
  105. static int FIG_poly_vec_cnt;
  106. static enum FIG_poly_stat FIG_polyvec_stat;
  107. /* 5 inches wide by 3 inches high */
  108. #define FIG_XMAX (5 * FIG_RES)
  109. #define FIG_YMAX (3 * FIG_RES)
  110.  
  111. #define FIG_XOFF (FIG_RES/4)
  112. #define FIG_YOFF (FIG_RES/4)
  113.  
  114.  
  115. #define BFIG_HTIC       (7*FIG_RES/80)
  116. #define BFIG_VTIC       (7*FIG_RES/80)
  117. #define BFIG_FONT_S     (16)    /* size in points */
  118. #define BFIG_VCHAR      FIG_to_pixel_v(BFIG_FONT_S) /* height in pixels of font */
  119. #define BFIG_HCHAR      FIG_to_pixel_h(BFIG_FONT_S) /* this is a guess at the width */
  120.  
  121. static  F_point FIG_points[100];
  122. static    F_line    FIG_line;
  123.  
  124. /* 8 inches wide by 5 inches high */
  125. #define BFIG_XMAX (8 * FIG_RES)
  126. #define BFIG_YMAX (5 * FIG_RES)
  127.  
  128. #define BFIG_XOFF (FIG_RES/2)
  129. #define BFIG_YOFF (FIG_RES/2)
  130.  
  131.  
  132. static int FIG_type;            /* negative types use real lines */
  133. static float FIG_spacing;       /* length of dash or dot spacing */
  134. static int FIG_justify;         /* Fig justification T_*_JUSTIFIED */
  135. static float FIG_angle;         /* Fig text angle 0=horiz, Pi/2=vert */
  136. static int FIG_use_color = FALSE;    /* do we use color or not? */
  137. static int FIG_is_big = FALSE;    /* big plot ? */
  138. static int FIG_color = DEFAULT;    /* which color to use */
  139. static int FIG_xoff = FIG_XOFF;
  140. static int FIG_yoff = FIG_YOFF;
  141. static int FIG_font_s = FIG_FONT_S;
  142.  
  143. static void FIG_poly_clean __P((enum FIG_poly_stat stat));
  144.  
  145.  
  146. #define FIG_POINT_TYPES POINT_TYPES /* we use the same points */
  147.  
  148.  
  149. static void
  150.   FIG_poly_clean(stat)
  151. enum FIG_poly_stat stat;
  152. {
  153.   int i,j;
  154.   if(stat == FIG_poly_part) {
  155.     fprintf(outfile, "%d %d %d %d %d %d %d %d %d %9.3f %d %d %d %d %d %d\n",
  156.      O_POLYLINE, FIG_line.type, FIG_line.style, FIG_line.thickness,
  157.      FIG_line.pen_color, FIG_line.fill_color, FIG_line.depth,
  158.      FIG_line.pen_style, FIG_line.fill_style, FIG_line.style_val,
  159.      FIG_line.join_style, FIG_line.cap_style, FIG_line.radius,
  160.      0, 0, FIG_poly_vec_cnt);
  161.  
  162.     j = 0;
  163.     for(i=0;i<FIG_poly_vec_cnt;i++)
  164.     {
  165.       fprintf(outfile," %d %d",FIG_points[i].x,FIG_points[i].y);
  166.       if (j++ > 10) { fprintf(outfile,"\n"); j = 0;}
  167.     }
  168.     if (j != 0) {fprintf(outfile,"\n");}
  169.   }
  170.   FIG_polyvec_stat = FIG_poly_new;
  171. }
  172.  
  173. TERM_PUBLIC void FIG_options()
  174. {
  175.     int parse_error=FALSE;
  176.  
  177.     FIG_use_color = FALSE;    /* default */
  178.     FIG_is_big = FALSE;    /* default */
  179.  
  180.     while (!END_OF_COMMAND) {
  181.         if (almost_equals(c_token,"m$onochrome")) {
  182.             FIG_use_color=FALSE;
  183.             c_token++;
  184.         }
  185.         else if (almost_equals(c_token,"c$olor")) {
  186.             FIG_use_color=TRUE;
  187.             c_token++;
  188.         }
  189.         else if (almost_equals(c_token,"s$mall")) {
  190.             FIG_is_big=FALSE;
  191.             c_token++;
  192.         }
  193.         else if (almost_equals(c_token,"b$ig")) {
  194.             FIG_is_big=TRUE;
  195.             c_token++;
  196.         }
  197.         else {
  198.             parse_error=TRUE;
  199.             int_error("expecting monochrome, color, small or big",
  200.                   c_token);
  201.         }
  202.     }
  203.  
  204.     sprintf(term_options,"%s %s", FIG_use_color ? "color" : "monochrome",
  205.                       FIG_is_big ? "big" : "small");
  206.  
  207.     if(!FIG_is_big) {
  208.         FIG_font_s=FIG_FONT_S;
  209.         term->xmax=FIG_XMAX;
  210.         term->ymax=FIG_YMAX;
  211.         term->v_char=FIG_to_pixel_v(FIG_font_s);
  212.         term->h_char=FIG_to_pixel_h(FIG_font_s);
  213.         term->v_tic=FIG_VTIC;
  214.         term->h_tic=FIG_HTIC;
  215.         FIG_xoff=FIG_XOFF;
  216.         FIG_yoff=FIG_YOFF;
  217.     } else {
  218.         FIG_font_s=BFIG_FONT_S;
  219.         term->xmax=BFIG_XMAX;
  220.         term->ymax=BFIG_YMAX;
  221.         term->v_char=FIG_to_pixel_v(FIG_font_s);
  222.         term->h_char=FIG_to_pixel_h(FIG_font_s);
  223.         term->v_tic=BFIG_VTIC;
  224.         term->h_tic=BFIG_HTIC;
  225.         FIG_xoff=BFIG_XOFF;
  226.         FIG_yoff=BFIG_YOFF;
  227.     }
  228.  
  229.     if(parse_error) {
  230.         int_error("expecting monochrome, color, small or big",
  231.               c_token);
  232.     }
  233. }
  234.  
  235. TERM_PUBLIC void FIG_init()
  236. {
  237.     FIG_posx = FIG_posy = 0;
  238.     FIG_polyvec_stat = FIG_poly_new;
  239.     FIG_linetype(-1);
  240.     FIG_justify_text(LEFT);
  241.     FIG_text_angle(0);
  242.  
  243.     FIG_line.tagged = FIG_DEFAULT;
  244.     FIG_line.distrib = FIG_DEFAULT;
  245.     FIG_line.type = T_POLYLINE;
  246.     FIG_line.style = 0;
  247.     FIG_line.thickness = 1;
  248.     FIG_line.fill_style = -1;
  249.     FIG_line.depth = 0;
  250.     FIG_line.pen_style =  0;
  251.     FIG_line.for_arrow = NULL;
  252.     FIG_line.back_arrow = NULL;
  253.     FIG_line.cap_style = 0;
  254.     FIG_line.join_style = 0;
  255.     FIG_line.style_val = 0.0;
  256.     FIG_line.radius = 0;
  257.     FIG_line.pic = NULL;
  258.     FIG_line.next = NULL;
  259.  
  260.     fprintf(outfile, "%s\n", FIG_MAGIC);
  261.     fprintf(outfile, "%s\n%s\n%s\n%d %d\n", FIG_ORIENT, FIG_JUST,
  262.     FIG_UNIT, FIG_RES, FIG_COORD_SYS);
  263. }
  264.  
  265.  
  266. TERM_PUBLIC void FIG_graphics()
  267. {
  268.     FIG_posx = FIG_posy = 0;
  269.     FIG_polyvec_stat = FIG_poly_new;
  270.     /* there is no way to have separate pictures in a FIG file */
  271. }
  272.  
  273.  
  274. TERM_PUBLIC void FIG_text()
  275. {
  276.     /* there is no way to have separate pictures in a FIG file */
  277.     FIG_poly_clean(FIG_polyvec_stat);
  278.     FIG_posx = FIG_posy = 0;
  279.     fflush(outfile);
  280. }
  281.  
  282.  
  283. /* Line types for FIG work like this:
  284.  *  for monochrome:
  285.  *  -2 : solid (border)
  286.  *  -1 : dotted 4 (axes)
  287.  *   0 : solid (first curve)
  288.  *   1 : dotted 3
  289.  *   2 : dashed 3
  290.  *   3 : dotted 6
  291.  *   4 : dashed 6
  292.  *   ... ...
  293.  *  for color, cycle through colors. once colors are used up, repeat colors
  294.  *   but start using dashed lines of different dash length. don't use white
  295.  *   as a color.
  296.  */
  297.  
  298. TERM_PUBLIC void FIG_linetype(linetype)
  299.         int linetype;                   /* expect linetype >= -2 */
  300. {
  301.     int last_FIG_type = FIG_type;
  302.     int last_FIG_spacing = FIG_spacing;
  303.     switch (linetype) {
  304.            case 0:
  305.            case -2: {
  306.                   FIG_type = SOLID_LINE;
  307.                   FIG_spacing = 0.0;
  308.           if (FIG_use_color) FIG_color = BLACK;
  309.                   break;
  310.            }
  311.            case -1: {
  312.                   FIG_type = DOTTED_LINE;
  313.                   FIG_spacing = 4.0; /* gap */
  314.           if (FIG_use_color) FIG_color = BLACK;
  315.                   break;
  316.            }
  317.            default: {
  318.                 linetype = abs(linetype); /* shouldn't be negative anyway */
  319.                 /* now linetype >= 1 */
  320.         if (FIG_use_color) {
  321.           FIG_type = (linetype >= WHITE);    /* dashed line */
  322.           FIG_color = linetype % WHITE;
  323.           FIG_spacing = (linetype / WHITE) * 3;
  324.         } else { /* monochrome */
  325.                   FIG_type = linetype % 2 + 1; /* dotted, dashed, ... */
  326.                   FIG_spacing = (linetype+1) / 2 * 3;
  327.         }
  328.                   break;
  329.            }
  330.     }
  331.     if (FIG_type != last_FIG_type || FIG_spacing != last_FIG_spacing)
  332.       FIG_poly_clean(FIG_polyvec_stat);
  333. }
  334.  
  335. TERM_PUBLIC void FIG_move(x,y)
  336.         unsigned int x,y;
  337. {
  338.     int last_FIG_posx = FIG_posx;
  339.     int last_FIG_posy = FIG_posy;
  340.     FIG_posx = x;
  341.     FIG_posy = y;
  342.     if (FIG_posx != last_FIG_posx || FIG_posy != last_FIG_posy)
  343.           FIG_poly_clean(FIG_polyvec_stat);
  344. }
  345.  
  346.  
  347. TERM_PUBLIC void FIG_vector(ux,uy)
  348.      unsigned int ux,uy;
  349. {
  350.   int x=ux, y=uy;
  351.  
  352.   if (FIG_polyvec_stat != FIG_poly_part)
  353.     {
  354.  
  355.     FIG_line.pen_color = FIG_color;
  356.     FIG_line.fill_color = FIG_color;
  357.     FIG_line.style = FIG_type;
  358.     FIG_line.style_val = FIG_spacing;
  359.     FIG_poly_vec_cnt = 0;
  360.     FIG_points[FIG_poly_vec_cnt].x=FIG_xoff + FIG_posx;
  361.     FIG_points[FIG_poly_vec_cnt].y=term->ymax
  362.      + FIG_yoff - FIG_posy;
  363.  
  364.         FIG_poly_vec_cnt = 1;
  365.         FIG_polyvec_stat = FIG_poly_part;
  366.     }
  367.   FIG_points[FIG_poly_vec_cnt].x = FIG_xoff +  x;
  368.   FIG_points[FIG_poly_vec_cnt].y = term->ymax + FIG_yoff - y;
  369.  
  370.   FIG_poly_vec_cnt++;
  371.   if (FIG_poly_vec_cnt > 99)
  372.     FIG_poly_clean(FIG_polyvec_stat);
  373.  
  374.   FIG_posx = x;
  375.   FIG_posy = y;
  376. }
  377.  
  378.  
  379. TERM_PUBLIC void FIG_arrow(sx, sy, ex, ey, head)
  380.         unsigned int sx, sy;     /* start coord */
  381.         unsigned int ex, ey;     /* end coord */
  382.     TBOOLEAN head;
  383. {
  384.     FIG_poly_clean(FIG_polyvec_stat);
  385.         fprintf(outfile, "%d %d %d %d %d %d %d %d %d %9.3f %d %d %d %d %d %d\n",
  386.          O_POLYLINE, FIG_line.type, FIG_line.style, FIG_line.thickness,
  387.          FIG_line.pen_color, FIG_line.fill_color, FIG_line.depth,
  388.          FIG_line.pen_style, FIG_line.fill_style, FIG_line.style_val,
  389.          FIG_line.join_style, FIG_line.cap_style, FIG_line.radius,
  390.          head ? 1 : 0, 0, 2);
  391.  
  392.         /* arrow line */
  393.     if ( head )
  394.             fprintf(outfile, "%d %d %.3f %.3f %.3f\n",
  395.                     0, 0, 1.0,
  396.             (double)(term->h_tic/2 + 1),
  397.             (double)term->h_tic        );
  398.         fprintf(outfile, "%d %d %d %d\n",
  399.                 FIG_xoff + sx, FIG_yoff + term->ymax - sy,
  400.         FIG_yoff + ex, FIG_yoff + term->ymax - ey);
  401.  
  402.         FIG_posx = ex;
  403.         FIG_posy = ey;
  404. }
  405.  
  406.  
  407. TERM_PUBLIC void FIG_put_text(x, y, str)
  408.         unsigned int x, y;
  409.         char *str;
  410. {
  411.   if (strlen(str) == 0) return;
  412.   FIG_poly_clean(FIG_polyvec_stat);
  413.   y -= term->v_char/2;    /* assuming vertical center justified */
  414.  
  415.     fprintf(outfile, "%d %d %d %d %d %d %6.3f %6.3f %d %6.3f %6.3f %d %d %s\\001\n",
  416.                   O_TEXT, FIG_justify, FIG_color, 0, FIG_DEFAULT,
  417.                   FIG_ROMAN_FONT, (float)FIG_font_s,
  418.           FIG_angle, SPECIAL_TEXT, (float)term->v_char,
  419.           (float)term->h_char*strlen(str),
  420.           FIG_xoff + x, term->ymax + FIG_yoff-y, str);
  421. }
  422.  
  423. TERM_PUBLIC int FIG_justify_text(mode)
  424.         enum JUSTIFY mode;
  425. {
  426.     switch(mode) {
  427.            case LEFT: FIG_justify = T_LEFT_JUSTIFIED; break;
  428.            case CENTRE: FIG_justify = T_CENTER_JUSTIFIED; break;
  429.            case RIGHT: FIG_justify = T_RIGHT_JUSTIFIED; break;
  430.            /* shouldn't happen */
  431.            default: FIG_justify = T_LEFT_JUSTIFIED; return (FALSE); break;
  432.     }
  433.     return (TRUE);
  434. }
  435.  
  436. TERM_PUBLIC int FIG_text_angle(angle)
  437.         int angle;
  438. {
  439.     if (angle)
  440.          FIG_angle = Pi / 2.0;  /* vertical is pi/2 radians */
  441.     else
  442.          FIG_angle = 0.0;               /* horizontal */
  443.     return (TRUE);
  444. }
  445.  
  446. TERM_PUBLIC void FIG_lpoint(x,y,number)
  447. unsigned int x,y;
  448. int number;
  449. {
  450.     FIG_type = 0; /* Solid lines for marker outline */
  451.     do_point(x,y,number);
  452. }
  453.  
  454. TERM_PUBLIC void FIG_reset()
  455. {
  456.     FIG_poly_clean(FIG_polyvec_stat);
  457.     FIG_posx = FIG_posy = 0;
  458.     fflush(outfile);
  459. }
  460. #endif /* TERM_BODY */
  461.  
  462. #ifdef TERM_TABLE
  463.  
  464. TERM_TABLE_START(fig_driver)
  465.    "fig", "FIG 3.1 graphics language: X graphics editor",
  466.          FIG_XMAX, FIG_YMAX, FIG_VCHAR, FIG_HCHAR, 
  467.          FIG_VTIC, FIG_HTIC, FIG_options, FIG_init, FIG_reset, 
  468.          FIG_text, null_scale, FIG_graphics, FIG_move, FIG_vector,
  469.          FIG_linetype, FIG_put_text, FIG_text_angle, FIG_justify_text,
  470.          FIG_lpoint, FIG_arrow, set_font_null
  471. TERM_TABLE_END(fig_driver)
  472.  
  473. #undef LAST_TERM
  474. #define LAST_TERM fig_driver
  475. #endif /* TERM_TABLE */
  476. #endif /* TERM_PROTO_ONLY */
  477.  
  478.  
  479. #ifdef TERM_HELP
  480. START_HELP(fig)
  481. "1 fig",
  482. "?set terminal fig",
  483. "?fig",
  484. " The `fig` terminal device has the options of either color or  monochrome",
  485. " and small or large image.  The default is monochrome and small.",
  486. "",
  487. " To select color, use:",
  488. "        set terminal fig color",
  489. "",
  490. " To select large, use:",
  491. "        set terminal fig large",
  492. "",
  493. " The large option is a substitute for the `bfig` terminal in earlier version,",
  494. " which is no longer supported."
  495. END_HELP(fig)
  496. #endif
  497.